package zzs.generalframework.console.core.authorization.authuserrolemap.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import zzs.generalframework.common.core.authorization.authuserrolemap.AuthUserRoleMapBO;
import zzs.generalframework.common.core.authorization.authuserrolemap.AuthUserRoleMapInfo;
import zzs.generalframework.common.core.authorization.authuserrolemap.AuthUserRoleMapPO;
import zzs.generalframework.console.common.exception.ConsoleException;
import zzs.generalframework.console.common.exception.ConsoleRollbackException;
import zzs.generalframework.console.common.model.ConsoleReturnBO;
import zzs.generalframework.console.common.model.ConsoleReturnBO.ConsoleResult;
import zzs.generalframework.console.core.authorization.authuserrolemap.dao.IAuthUserRoleMapDao;
import zzs.generalframework.console.core.authorization.authuserrolemap.service.IAuthUserRoleMapService;
import zzs.generalframework.util.core.reflect.BeanUtils;

/**
 * @title
 * @description
 * @project zzs-console
 * @package zzs.generalframework.console.core.authorization.authuserrolemap.service.impl
 * @author zzs
 * @date 2018年2月2日
 * @version 1.0
 */
@Service("zzs.generalframework.console.core.authorization.authuserrolemap.service.impl.authUserRoleMapServiceImpl")
public class AuthUserRoleMapServiceImpl implements IAuthUserRoleMapService
{

	private Logger logger = LoggerFactory.getLogger(AuthUserRoleMapServiceImpl.class);
	@Autowired
	BeanUtils beanUtils;
	@Autowired
	IAuthUserRoleMapDao authUserRoleMapDao;

	@Override
	public ConsoleReturnBO saveAuthUserRoleMap(AuthUserRoleMapBO authUserRoleMapBO)
	{
		logger.debug("====保存权限用户角色映射信息开始====");
		logger.debug("====保存权限用户角色映射信息.入参:AuthUserRoleMapBO=[{}]====", authUserRoleMapBO);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		AuthUserRoleMapPO authUserRoleMapPO = new AuthUserRoleMapPO();
		copyAuthUserRoleMapBO2AuthUserRoleMapPO(authUserRoleMapBO, authUserRoleMapPO);
		Boolean insertResult = authUserRoleMapDao.insert(authUserRoleMapPO);
		if (insertResult)
		{
			returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
		}
		logger.debug("====保存权限用户角色映射信息.出参:ConsoleReturnBO=[{}]====", returnConsoleReturnBO);
		logger.info("====保存权限用户角色映射信息结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public ConsoleReturnBO saveAuthUserRoleMap(List<AuthUserRoleMapBO> authUserRoleMapBOList) throws ConsoleException
	{
		logger.info("====保存权限用户角色映射信息列表开始====");
		logger.debug("====保存权限用户角色映射信息列表.入参:AuthUserRoleMapBOList=[{}]====", authUserRoleMapBOList);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
		for (AuthUserRoleMapBO itemBO : authUserRoleMapBOList)
		{
			ConsoleReturnBO itemResult = saveAuthUserRoleMap(itemBO);
			if (ConsoleResult.FAILD.equals(itemResult.getResult()))
			{
				returnConsoleReturnBO.setResult(ConsoleResult.FAILD);
				returnConsoleReturnBO.setMessage(itemResult.getMessage());
				throw new ConsoleRollbackException();
			}
		}
		logger.debug("====保存权限用户角色映射信息列表.出参:ConsoleReturnBO=[{}]====", returnConsoleReturnBO);
		logger.info("====保存权限用户角色映射信息列表结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public ConsoleReturnBO getAuthUserRoleMap(AuthUserRoleMapInfo authUserRoleMapInfo)
	{
		logger.info("====获取权限用户角色映射信息开始====");
		logger.debug("====获取权限用户角色映射信息.入参:AuthUserRoleMapInfo=[{}]====", authUserRoleMapInfo);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		List<AuthUserRoleMapPO> authUserRoleMapPOList = authUserRoleMapDao.select(authUserRoleMapInfo);
		if (authUserRoleMapPOList == null || authUserRoleMapPOList.size() == 0)
		{
			returnConsoleReturnBO.setMessage("未获取到记录");
		}
		else if (authUserRoleMapPOList.size() == 1)
		{
			returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
			AuthUserRoleMapBO authUserRoleMapBO = new AuthUserRoleMapBO();
			copyAuthUserRoleMapPO2AuthUserRoleMapBO(authUserRoleMapPOList.get(0), authUserRoleMapBO);
			returnConsoleReturnBO.setReturnObj(authUserRoleMapBO);
		}
		else
		{
			returnConsoleReturnBO.setMessage("记录不唯一");
			AuthUserRoleMapBO authUserRoleMapBO = new AuthUserRoleMapBO();
			copyAuthUserRoleMapPO2AuthUserRoleMapBO(authUserRoleMapPOList.get(0), authUserRoleMapBO);
			returnConsoleReturnBO.setReturnObj(authUserRoleMapBO);
		}
		logger.debug("====获取权限用户角色映射信息.出参:ConsoleReturnBO=[{}]====", returnConsoleReturnBO);
		logger.info("====获取权限用户角色映射信息结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public ConsoleReturnBO listAuthUserRoleMap(AuthUserRoleMapInfo authUserRoleMapInfo)
	{
		logger.info("====获取权限用户角色映射信息列表开始====");
		logger.debug("====获取权限用户角色映射信息列表.入参:AuthUserRoleMapInfo=[{}]====", authUserRoleMapInfo);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		List<AuthUserRoleMapPO> authUserRoleMapPOList = authUserRoleMapDao.select(authUserRoleMapInfo);
		if (authUserRoleMapPOList == null || authUserRoleMapPOList.size() == 0)
		{
			returnConsoleReturnBO.setMessage("未获取到记录");
		}
		else
		{
			returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
			List<AuthUserRoleMapBO> resultList = new ArrayList<AuthUserRoleMapBO>();
			for (AuthUserRoleMapPO itemPO : authUserRoleMapPOList)
			{
				AuthUserRoleMapBO itemBO = new AuthUserRoleMapBO();
				copyAuthUserRoleMapPO2AuthUserRoleMapBO(itemPO, itemBO);
				resultList.add(itemBO);
			}
			returnConsoleReturnBO.setReturnObj(resultList);
		}
		logger.debug("====获取权限用户角色映射信息列表.出参:ConsoleReturnBO＝[{}]====", returnConsoleReturnBO);
		logger.info("====获取权限用户角色映射信息列表结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public ConsoleReturnBO updateAuthUserRoleMap(AuthUserRoleMapBO authUserRoleMapBO)
	{
		logger.info("====更新权限用户角色映射信息开始====");
		logger.debug("====更新权限用户角色映射信息.入参:AuthUserRoleMapBO=[{}]====", authUserRoleMapBO);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		AuthUserRoleMapPO authUserRoleMapPO = new AuthUserRoleMapPO();
		copyAuthUserRoleMapBO2AuthUserRoleMapPO(authUserRoleMapBO, authUserRoleMapPO);
		Boolean updateResult = authUserRoleMapDao.update(authUserRoleMapPO);
		if (updateResult)
		{
			returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
		}
		logger.debug("====更新权限用户角色映射信息.出参:ConsoleReturnBO=[{}]====", returnConsoleReturnBO);
		logger.info("====更新权限用户角色映射信息结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public ConsoleReturnBO updateAuthUserRoleMap(List<AuthUserRoleMapBO> authUserRoleMapBOList) throws ConsoleException
	{
		logger.info("====更新权限用户角色映射信息列表开始====");
		logger.debug("====更新权限用户角色映射信息列表.入参:List<AuthUserRoleMapBO>=[{}]====", authUserRoleMapBOList);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
		for (AuthUserRoleMapBO itemBO : authUserRoleMapBOList)
		{
			ConsoleReturnBO itemResult = updateAuthUserRoleMap(itemBO);
			if (ConsoleResult.FAILD.equals(itemResult.getResult()))
			{
				returnConsoleReturnBO.setResult(ConsoleResult.FAILD);
				returnConsoleReturnBO.setMessage(itemResult.getMessage());
				throw new ConsoleRollbackException();
			}
		}
		logger.debug("====更新权限用户角色映射信息列表.出参:ConsoleReturnBO=[{}]====", returnConsoleReturnBO);
		logger.info("====更新权限用户角色映射信息列表结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public ConsoleReturnBO removeAuthUserRoleMap(AuthUserRoleMapBO authUserRoleMapBO)
	{
		logger.info("====删除权限用户角色映射信息开始====");
		logger.debug("====删除权限用户角色映射信息.入参:AuthUserRoleMapBO＝[{}]======", authUserRoleMapBO);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		AuthUserRoleMapPO authUserRoleMapPO = new AuthUserRoleMapPO();
		copyAuthUserRoleMapBO2AuthUserRoleMapPO(authUserRoleMapBO, authUserRoleMapPO);
		Boolean deleteResult = authUserRoleMapDao.delete(authUserRoleMapPO);
		if (deleteResult)
		{
			returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
		}
		logger.debug("====删除权限用户角色映射信息.出参:ConsoleReturnBO=[{}]====", returnConsoleReturnBO);
		logger.info("====删除权限用户角色映射信息结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public ConsoleReturnBO removeAuthUserRoleMap(List<AuthUserRoleMapBO> authUserRoleMapBOList) throws ConsoleException
	{
		logger.info("====删除权限用户角色映射信息列表开始====");
		logger.debug("====删除权限用户角色映射信息列表.入参:List<AuthUserRoleMapBO>＝[{}]======", authUserRoleMapBOList);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
		for (AuthUserRoleMapBO itemBO : authUserRoleMapBOList)
		{
			ConsoleReturnBO itemResult = removeAuthUserRoleMap(itemBO);
			if (ConsoleResult.FAILD.equals(itemResult.getResult()))
			{
				returnConsoleReturnBO.setResult(ConsoleResult.FAILD);
				returnConsoleReturnBO.setMessage(itemResult.getMessage());
				throw new ConsoleRollbackException();
			}
		}
		logger.debug("====删除权限用户角色映射信息列表.出参:ConsoleReturnBO＝[{}]====", returnConsoleReturnBO);
		logger.info("====删除权限用户角色映射信息列表结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public Long countAuthUserRoleMap(AuthUserRoleMapBO authUserRoleMapBO)
	{
		logger.info("====统计权限用户角色映射信息开始====");
		logger.debug("====统计权限用户角色映射信息。入参：AuthUserRoleMapBO＝[{}]======", authUserRoleMapBO);
		Long returnLong = new Long(0);
		AuthUserRoleMapPO authUserRoleMapPO = new AuthUserRoleMapPO();
		copyAuthUserRoleMapBO2AuthUserRoleMapPO(authUserRoleMapBO, authUserRoleMapPO);
		returnLong = authUserRoleMapDao.count(authUserRoleMapPO);
		logger.debug("====统计权限用户角色映射信息.出参:Long＝[{}]======", returnLong);
		logger.info("====统计权限用户角色映射信息结束====");
		return returnLong;
	}

	private void copyAuthUserRoleMapBO2AuthUserRoleMapPO(AuthUserRoleMapBO authUserRoleMapBO, AuthUserRoleMapPO authUserRoleMapPO)
	{
		beanUtils.copyBeanNotNull(authUserRoleMapBO, authUserRoleMapPO);
		if (authUserRoleMapBO.getSysID() != null)
		{
			authUserRoleMapPO.setSeqID(authUserRoleMapBO.getSysID());
		}
	}

	private void copyAuthUserRoleMapPO2AuthUserRoleMapBO(AuthUserRoleMapPO authUserRoleMapPO, AuthUserRoleMapBO authUserRoleMapBO)
	{
		beanUtils.copyBeanNotNull(authUserRoleMapPO, authUserRoleMapBO);
		if (authUserRoleMapPO.getSeqID() != null)
		{
			authUserRoleMapBO.setSysID(authUserRoleMapPO.getSeqID());
		}
	}

}
